
<script type="text/javascript">
var flag = false;

	$(document).ready(function() {
		$("#documents").tablesorter({
			widthFixed: true,
			widgets: ['zebra']
		}).tablesorterPager({
			container: $(".pager")
		});
	});
	
	jQuery(document).ajaxStart(function() {
		var progress = noty({
			id: "progress",
			text: $(document.createElement('div')).append(
					$(document.createElement('img')).attr({
						width : "12",
						height : "12",
						alt: "",
						src: "<?php echo base_url().'theme/default/images/loading.gif'; ?>"
					}),
					$(document.createElement('span')).text("  memproses request...")
				),
			type: "alert",
			layout: "top",
			theme: "default",
			timeout: 5000,
			animation: {
				open: {height: 'toggle'},
			    close: {height: 'toggle'},
			    easing: 'swing',
				speed: 200
			}
		});
	});
	
	jQuery(document).ajaxStop(function() {
		$.noty.close("progress");
	});

	function download_file(id) {
		document.location.href = "<?php echo site_url('mahasiswa/profile/document/download/').'/'; ?>" + id;
	}

	function upload_file() {
		document.location.href = "<?php echo site_url('mahasiswa/profile/document/upload'); ?>";
	}

	function request_delete_file(id) {
		var token = $("input[name=smb_36742cef0aed]").val();
		$.ajax({
			type: "POST",
			url: "<?php echo site_url('mahasiswa/profile/document/delete'); ?>",
			data: { id : id, 'smb_36742cef0aed' : token },
			success: function(data) {
				var n = noty({
					text: data,
					type: "warning",
					dismissQueue: true,
					layout: 'center',
					theme: 'default',
					timeout: 1000,
					animation: {
						open: {height: 'toggle'},
					    close: {height: 'toggle'},
					    easing: 'swing',
						speed: 200
					}
				});
				$("#f" + id).remove();
				$("#info").remove();
				var rowCount = $("#documents tr").length;
				if (rowCount == 1) {
					$("#documents").append(
						$(document.createElement('tr')).append(
							$(document.createElement('td'))
								.attr({ align : "center", colspan : "4" })
								.append(
									$(document.createElement('span'))
										.addClass("info")
										.text("Belum ada berkas diupload.")
								)
						)
					);
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				var n = noty({
					text: errorThrown,
					type: "error",
					dismissQueue: true,
					layout: 'center',
					theme: 'default',
					timeout: 1000,
					animation: {
						open: {height: 'toggle'},
					    close: {height: 'toggle'},
					    easing: 'swing',
						speed: 200
					}
				});
			}
		});
	}

	function delete_file(id, name) {
		if (flag == false) {
			var n = noty({
				text: "Hapus entri dokumen " + name + "?",
				type: "warning",
				dismissQueue: true,
				layout: "center",
				theme: "default",
				buttons: [
					{addClass: 'btn btn-primary', text: 'Ok', onClick: function($noty) {
						$noty.close();
						flag = false;
						request_delete_file(id);}
					},
					{addClass: 'btn btn-danger', text: 'Cancel', onClick: function($noty) {
						$noty.close();
						flag = false;}
					}
				]
			});
		
			flag = true;
		}
		return false;		
	}
	
</script>

<div id="main-content" class="prepend-1 span-19 last">
<?php 
	if ($downloadstatus == 'failed') {
		echo '<span id="info" class="error">Berkas '.$notfoundfile.' tidak ditemukan. Silakan upload ulang berkas tersebut.<br /><br /></span>';
	} else if ($downloadstatus == 'success') {
		
	}
	if ($uploadstatus != FALSE) {
		echo $uploadstatus;
	}
	if ($deletestatus == 'success') {
		echo '<span id="info" class="info">Berkas '.$deletefile.' berhasil dihapus.<br /><br /></span>';
	} else if ($deletestatus == 'failed') {
		echo '<span id="info" class="error">Berkas '.$deletefile.' gagal dihapus. Silakan coba beberapa saat lagi.<br /><br /></span>';
	}
?>
	<h3>Daftar Berkas</h3>
	<input type="button" class="submit" name="upload" value="Upload Berkas" onclick="upload_file()" />
	<br />
<?php echo form_open(); ?>
	<table id="documents">
	<thead>
		<tr>
			<th width="250">Kategori Berkas</th>
			<th width="150">Tanggal Upload</th>
			<th>Keterangan</th>
			<th width="190">Operasi</th>
		</tr>
	</thead>
	<tfoot>
		<?php echo $pager; ?>
	</tfoot>
	<tbody>
<?php if ($files == NULL) : ?>
		<tr>
			<td colspan="4" align="center">
				<span class="info">Belum ada berkas diupload.</span>
			</td>
		</tr>
<?php 
	else :
		foreach ($files as $file) :
			$time = mysql_to_unix($file->upload_time);  
?>
		<tr id="f<?php echo $file->id; ?>">
			<td><?php echo $file->name; ?></td>
			<td align="center"><?php echo mdate(DATE_FORMAT, $time); ?></td>
			<td><?php echo $file->details; ?></td>
			<td align="center">
				<input class="submit" type="button" name="download" value="Download" onclick="download_file('<?php echo $file->id;?>');" />
				<?php echo nbs().'|'.nbs(); ?>
				<input class="submit" type="button" name="delete" value="Delete" onclick="delete_file('<?php echo $file->id; ?>', '<?php echo $file->name; ?>');" />
			</td>
		</tr>
<?php
		endforeach; 
	endif; 
?>
	</tbody>
	</table>
<?php echo form_close(); ?>
	<div class="clear"><?php echo nbs(); ?></div>
</div>
